//
//  ViewController.m
//  booklx
//
//  Created by MAC on 15-12-14.
//  Copyright (c) 2015年 me. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSArray * imgs;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"书架";
    //去掉滚动条
    self.tableView.showsVerticalScrollIndicator=NO;
    NSBundle * bundle = [NSBundle mainBundle];
    imgs = [bundle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    
    //高度
    //self.tableView.rowHeight = 139;
    
    //去掉线条
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 3;
    if (!imgs || imgs.count == 0) {
        return 0;
    }
    NSInteger count = imgs.count - 1;
    return count/3 + 1;
}


// 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    UITableViewCell * cell = [[UITableViewCell alloc]init];
    NSInteger index = row * 3;
    for (int i = 0; i < 3 && index + i < imgs.count; i++) {
        UIImage * img = [UIImage imageNamed:[imgs[index + i]lastPathComponent]];
        UIImageView * iv = [[UIImageView alloc]initWithImage:img];
        CGFloat edge = 15;
        CGFloat width = (self.view.frame.size.width - edge*4)/3;
        iv.frame = CGRectMake(edge+(width+edge)*i, 8, width, 139-8- 8*2);
        [cell addSubview:iv];
    }
    
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
    
    return cell;
    //NSMutableArray * arr = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
